//池子
import {
  vProps,
  vEvents
} from './propsType';

export default function pools(dom, methods) {
  // console.log(methods);
  //{showImg1: ƒ, showImg2: ƒ}

  const propsPool = new Map();
  const eventsPool = new Map();
  const allNodes = dom.getElementsByTagName('*');
  // console.log(allNodes);
  //HTMLCollection(5) [div, img.img, img.img, button, button]
  const {
    vIf,
    vShow
  } = vProps;
  const {
    vClick
  } = vEvents;

  let node = null;

  for (let i = 0; i < allNodes.length; i++) {
    node = allNodes[i];

    const vIfVal = node.getAttribute(vIf);
    const vShowVal = node.getAttribute(vShow);
    const vClickVal = node.getAttribute(`@${vClick}`);

    //绑定当前node和type和prop
    if (vIfVal) {
      propsPool.set(node, {
        type: vIf,
        prop: vIfVal
      });

      //移除属性
      node.removeAttribute(vIf);
    } else if (vShowVal) {
      propsPool.set(node, {
        type: vShow,
        prop: vShowVal
      });

      node.removeAttribute(vShow);
    } else if (vClickVal) {
      eventsPool.set(node, {
        type: vClick,
        handler: methods[vClickVal]
      });

      node.removeAttribute(vClick);
    }
  }

  return {
    propsPool,
    eventsPool
  }
}